﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class page_GoodType_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadAction();               //保存传过来的参数            
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        base.OnLoad(e);
    }

    #endregion

    protected void btnCancel_Click(object sender, EventArgs e)
    {       
        Response.Redirect("GoodType_Manager.aspx");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DAL.Tables.tbl_Shop_GoodType goodType = new DAL.Tables.tbl_Shop_GoodType();
        goodType.TypeName.Value = tbNewType.Text;
        if (ddlType.SelectedValue == "--无--")
        {
            goodType.ParentID.Value = "00";
        }
 
        //goodType.TypeKind.Value = Convert.ToInt32(rblTypeKind.SelectedValue);
        goodType.TypeStatus.Value = Convert.ToInt32(rblTypeStatus.SelectedValue);
        goodType.Introduce.Value = txtIntroduce.Text;
        goodType.Remark.Value = txtRemark.Text;

        if (cbIsSelf.Visible == true && cbIsSelf.Checked == false)
        {
            goodType.ShopID.Value = _Site.ID;
        }

        if (tbNewType.Text.Equals(""))
        {
            Shove._Web.JavaScript.Alert(this.Page, "商品类别名称不能为空！");
        }

        if (this.hAction.Value == "Edit")           //修改
        {
            long i = goodType.Update("[ID]='" + hID.Value + "'");

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改商品类别成功！", "GoodType_Manager.aspx");
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改商品类别失败！");
            }                  
        }
        if (this.hAction.Value == "Add")            //添加
        {
            string ReturnDescription = "";

            long i = DAL.Procedures.P_Shop_AddGoodType(ddlType.SelectedValue, tbNewType.Text, Shove._Convert.StrToShort(rblTypeKind.SelectedValue, 0), Shove._Convert.StrToShort(rblTypeStatus.SelectedValue,0), txtIntroduce.Text, txtRemark.Text, _Site.ID, ref ReturnDescription);

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "添加商品类别成功！", "GoodType_Manager.aspx");
            }
            else 
            {
                Shove._Web.JavaScript.Alert(this.Page, "添加商品类别失败！");
            }                                             
        }
    }

    /// <summary>
    /// 梆定商品类别
    /// </summary>
    private void LoadGoodType()
    {
        string Fields = "ID,TypeName,";
        Fields += "(case when TypeStatus ='1' then '正常' else '停用' end )as TypeStatus,ParentID,Introduce";
        string Condition = "(ShopID=" + _Site.ID + ")";
        string Sort = "";

        if (!ID.Equals(""))
        {
            Condition = "(ShopID=" + _Site.ID + ")  and (substring(ID,1,len('" + "" + "')))='" + "" + "'";
        }

        DataTable dt = new DAL.Tables.tbl_Shop_GoodType().Open(Fields, Condition, Sort);

        ddlType.DataSource = dt;
        ddlType.DataTextField = "TypeName";
        ddlType.DataValueField = "ID";
        ddlType.DataBind();
        ddlType.Items.Insert(0,new ListItem("--无--","00"));
    }

    /// <summary>
    /// 保存传过来的参数
    /// </summary>
    private void ReadAction()
    {
        this.hAction.Value = Shove._Web.Utility.GetRequest("Action");
        this.hID.Value = Shove._Web.Utility.GetRequest("ID");

        if (this.hAction.Value == "Edit")
        {
            BindData();
        }
        if (this.hAction.Value == "Add")
        {
            LoadGoodType();     //梆定所有父类别
        }
    }

    /// <summary>
    /// 编辑状态时，初始化
    /// </summary>
    private void BindData()
    {
        LoadGoodType();
        //初始化父类   
        string ID = hID.Value;

        string ParentName = "";
        //string Condition = "ID = substring('" + ID + "',1,len('" + ID + "')-2)";
        string Condition = "ID = '" + ID + "'";
        DataTable dt = new DAL.Tables.tbl_Shop_GoodType().Open("[ID],[TypeName]", Condition, "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无您需要的数据", this.GetType().BaseType.FullName);

            return;
        }

        try
        {
            ParentName = dt.Rows[0]["TypeName"].ToString();
        }
        catch
        {
            ParentName = "--无--";
        }

        this.ddlType.Items.Add(ParentName);
        this.ddlType.SelectedValue = ParentName;
        this.ddlType.Enabled = false;

        dt = new DAL.Tables.tbl_Shop_GoodType().Open("", "[ID]='" + ID + "'", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无您需要的数据", this.GetType().BaseType.FullName);

            return;
        }

        if (dt != null)
        {
            tbNewType.Text = dt.Rows[0]["TypeName"].ToString();
            //rblTypeKind.SelectedValue = dt.Rows[0]["TypeKind"].ToString();
            rblTypeStatus.SelectedValue = dt.Rows[0]["TypeStatus"].ToString();
            txtIntroduce.Text = dt.Rows[0]["Introduce"].ToString();
            txtRemark.Text = dt.Rows[0]["Remark"].ToString();

            if (Shove._Convert.StrToLong(dt.Rows[0]["ShopID"].ToString(), -1) != -1)
            {
                cbIsSelf.Checked = true;
            }
        }
    }

}
